package cn.lsnu.modules.infos.controller;

import cn.lsnu.common.interfacetools.interfacecontroller.BaseController;
import cn.lsnu.common.interfacetools.interfaceentity.Page;
import cn.lsnu.common.sys.entity.MenuEntity;
import cn.lsnu.common.sys.service.MenuService;
import cn.lsnu.modules.infos.entity.InfosEntity;
import cn.lsnu.modules.infos.service.InfosService;
import cn.lsnu.utils.Global;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;


@Controller
@RequestMapping({"/app/infos","/infos"})
public class InfosController extends BaseController {

    @Autowired
    private InfosService infosService;

    @Autowired
    private MenuService menuService;

    @RequestMapping({"/infopages","/listjson"})
    public
    @ResponseBody    Page<InfosEntity>
    infoPages(InfosEntity entity) throws Exception{
        Page<InfosEntity> infosList = infosService.findPage(entity);
        return infosList;
    }

    @RequestMapping("/list")
    public String listInfos(Model model, HttpSession session, MenuEntity entity)throws Exception{
        String firstTitle = entity.getFirstTitle();
        //每个-list页面的id
        model.addAttribute("pageId",entity.getMenuId());
        //获取页面的所有信息
        String userCode = (String) session.getAttribute(Global.SESSION_ID);
        entity.setUserCode(userCode);
        entity = menuService.getPageMenu(entity);
        entity.setFirstTitle(firstTitle);
        entity.setSecondTitle("信息管理列表");
        model.addAttribute("pageMenuMsg",entity);
        return "webpage/modules/infos/infos-list";
    }

    @RequestMapping("/del")
    public @ResponseBody InfosEntity
    delete(InfosEntity entity)throws Exception{
        infosService.delete(entity);
        entity.setCode(Global.STATUE_SUCCESS);
        entity.setMsg("删除成功");
        return entity;
    }


    @RequestMapping("/save")
    public @ResponseBody InfosEntity
    save(InfosEntity entity)throws Exception{
        infosService.save(entity);
        entity.setCode(Global.STATUE_SUCCESS);
        entity.setMsg("插入成功");
        return entity;
    }

    @RequestMapping("/updmsg")
    public String updMsg(String key,Model model)throws Exception{
        InfosEntity entity = infosService.get(key);
        model.addAttribute("updinfos",entity);
        return "webpage/modules/infos/infos-upd";
    }

    @RequestMapping("/upd")
    public void upd(InfosEntity entity)throws Exception{
       infosService.update(entity);
    }


}
